import 'dart:convert';
import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/services.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:uuid/v4.dart';
import 'package:winkai/res/constant.dart';
import 'package:winkai/entity/language_info.dart';
import 'package:winkai/entity/user_info.dart';
import 'package:winkai/ext/ext.dart';

class ValueHelper {
  ValueHelper._() {
    getLanguageInfo().then((value) {
      languageInfo = value;
    });
  }

  static final ValueHelper _instance = ValueHelper._();

  static ValueHelper get instance => _instance;

  LanguageInfo? languageInfo;
  var sendFreeMessageCount = 0;
  static const _deviceIdChannel = MethodChannel('app.get.channel');

  final _storage = const FlutterSecureStorage(
    aOptions: AndroidOptions(encryptedSharedPreferences: true),
    iOptions: IOSOptions(),
  );

  ///获取设备ID
  Future<String> getDeviceId() async {
    var devicesId = await _storage.read(key: Constant.deviceId);
    if (devicesId.isNullOrEmpty) {
      devicesId = await _getDeviceIdAsync();
      await _storage.write(key: Constant.deviceId, value: devicesId);
    }
    return devicesId!;
  }

  ///获取手机端用户设备标识
  Future<String> _getDeviceIdAsync() async {
    String getCustomDeviceId() {
      String? deviceNo = const UuidV4().generate();
      return deviceNo;
    }

    if (Platform.isAndroid) {
      final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      final androidInfo = await deviceInfo.androidInfo;
      return androidInfo.id.isNotEmpty == true
          ? androidInfo.id
          : getCustomDeviceId();
    } else if (Platform.isIOS) {
      final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      final iosInfo = await deviceInfo.iosInfo;
      return iosInfo.identifierForVendor?.isNotEmpty == true
          ? iosInfo.identifierForVendor!
          : getCustomDeviceId();
    } else {
      return getCustomDeviceId();
    }
  }

  ///存储当前语言
  Future<void> saveLanguage(LanguageInfo languageInfo) {
    this.languageInfo = languageInfo;
    return _storage.write(
      key: Constant.currentLanguage,
      value: languageInfo.toString(),
    );
  }

  ///获取当前语言
  Future<LanguageInfo?> getLanguageInfo() async {
    LanguageInfo? languageInfo;
    try {
      var languageStr = await _storage.read(
        key: Constant.currentLanguage,
      );
      languageInfo = LanguageInfo.fromJson(jsonDecode(languageStr ?? ''));
    } catch (e) {}
    this.languageInfo = languageInfo;
    return languageInfo;
  }

  ///保存登录信息
  Future<void> savaUserInfo(UserInfo? userModel) async {
    if (userModel != null) {
      await _storage.write(
          key: Constant.loginInfo, value: userModel.toString());
    }
  }

  ///获取登录信息
  Future<UserInfo?> getUserInfo() async {
    UserInfo? userModel;
    try {
      final userStr = await _storage.read(key: Constant.loginInfo);
      if (userStr != null) {
        userModel = UserInfo.fromJson(jsonDecode(userStr ?? ''));
      }
    } catch (e) {}
    return userModel;
  }

  ///获取已发送的免费消息数量
  Future<int> getSendFreeMessageCount() async {
    try {
      var str = await _storage.read(key: Constant.sendFreeMessageCount);
      sendFreeMessageCount = int.tryParse(str ?? '') ?? sendFreeMessageCount;
    } catch (e) {}
    return sendFreeMessageCount;
  }

  ///记录发送的免费消息数量
  Future<void> addSendFreeMessageCount() async {
    return _storage.write(
      key: Constant.sendFreeMessageCount,
      value: (sendFreeMessageCount + 1).toString(),
    );
  }
}
